# -*- coding: utf-8 -*-
import csv
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import time
import pandas as pd #读取csv文件的库

pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)


header = ["评价", "评分"]
f=open("香港迪士尼.csv",mode="w",newline="",encoding='utf-8')#改成你自己需要的文件名
csvwriter=csv.writer(f)
csvwriter.writerow(header)
# with open('output.csv', 'a', newline='', encoding='utf-8') as csvfile:
#     csvwriter = csv.writer(csvfile)
#     # 写入数据
#     csvwriter.writerow(header)

url="https://you.ctrip.com/sight/hongkong38/22943.html"#目标景区的携程网页地址

def scrolldown(driver,begin,step,times):

    scroll_y=begin

    for i in range(times):
        time.sleep(1)
        #用js进行滑动
        driver.execute_script("window.scrollTo(0,{})".format(scroll_y))
        scroll_y+=step



def main () :
    driver = webdriver.Edge()

    # 假设 'url' 是您想要打开的网址
    driver.get ( url )
    driver.maximize_window ()
    time.sleep ( 3 )

    while True :
        user_input = input ( "请输入1以说明登陆成功: " )
        if user_input == "1" :
            break


    time.sleep ( 2 )
    scrolldown ( driver, 1000, 1000, 10 )

    time_sort_btn = driver.find_element ( "xpath",
                                          '//*[@id="commentModule"]/div[@class="sortList"]/span[@class="sortTag"]' )
    time_sort_btn.click ()
    max_pages=56845
    get_comments(driver,max_pages)


def get_comments(driver,max_pages):
    page = 1
    while page <= max_pages:
        commentList=driver.find_element("xpath",'//*[@id="commentModule"]/div[@class="commentList"]')
        comment_items=commentList.find_elements("xpath",'./div[@class="commentItem"]')
        for c in comment_items:
            comment_info=c.find_element("xpath",'./div[@class="contentInfo"]/div[@class="commentDetail"]').text
            rating=c.find_element("xpath",'./div[@class="contentInfo"]/div[@class="scroreInfo"]/span[@class="averageScore"]').text
            rating = ''.join ( filter ( str.isdigit, str(rating) ) )
            try:
                print(comment_info,rating,page)
            except UnicodeEncodeError:
                print(rating)
            csvwriter.writerow ( [comment_info, rating] )
        next_btn=driver.find_element("xpath",'//*[@id="commentModule"]/div[@class="myPagination"]/ul[@class="ant-pagination"]/li[@title="下一页"]/span[@class="ant-pagination-item-comment"]')
        driver.execute_script ( "arguments[0].click();", next_btn )
        page +=1

# from selenium import webdriver
# from selenium.webdriver.common.by import By
# from selenium.webdriver.edge.service import Service as EdgeService
# from selenium.webdriver.chrome.service import Service as ChromeService
# import time
# import csv


# def get_comments(driver, max_pages):
#         current_page = 1
#         while current_page <= max_pages:
#             try:
#                 commentList = driver.find_element("xpath", '//*[@id="commentModule"]/div[@class="commentList"]')
#                 comment_items = commentList.find_elements("xpath", './div[@class="commentItem"]')
#                 for c in comment_items:
#                     comment_info = c.find_element("xpath", './div[@class="contentInfo"]/div[@class="commentDetail"]').text
#                     rating = c.find_element("xpath", './div[@class="contentInfo"]/div[@class="scroreInfo"]/span[@class="averageScore"]').text
#                     rating = ''.join(filter(str.isdigit, str(rating)))
#                     csvwriter.writerow([comment_info, rating])
#             except Exception as e:
#                 print(f"Error retrieving comments: {e}")
#                 break
#
#             try:
#                 next_btn = driver.find_element("xpath", '//*[@id="commentModule"]/div[@class="myPagination"]/ul[@class="ant-pagination"]/li[@title="下一页"]/span[@class="ant-pagination-item-comment"]')
#                 driver.execute_script("arguments[0].click();", next_btn)
#                 time.sleep(2)
#                 current_page += 1
#             except Exception as e:
#                 print(f"Error finding next page button: {e}")
#                 break
#
# def main():
#     # 假设 'url' 是您想要打开的网址
#     driver = webdriver.Edge()  # 或者使用 Chrome
#     driver.get(url)
#     driver.maximize_window()
#     time.sleep(3)
#
#     user_input = input("请输入1以说明登陆成功: ")
#     if user_input == "1":
#         time.sleep(2)
#         scrolldown(driver, 1000, 1000, 10)
#         time_sort_btn = driver.find_element("xpath", '//*[@id="commentModule"]/div[@class="sortList"]/span[@class="sortTag"]')
#         time_sort_btn.click()
#         get_comments(driver, 10)  # 假设我们想要获取56页的评论
#     else:
#         print("登录失败，程序退出。")
#
#     # 关闭浏览器
#     driver.quit()

if __name__ == "__main__":
    main()
